package org.easy.mybatisplus.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * mybatisplus分页入参增强
 * <p>
 * 增加接收参数方便提供给mybatisplus使用
 * <pre>
 *     class XXXController {
 *        @RequestMapping(xxxx)
 *        public xxx page(@RequestBody PageQuery<Entity> pageQuery)
 *     }
 *
 *      class XXXService{
 *        public xxx page(){
 *            PageReq<Entity> page = xxxDao.selectPage((PageReq<Entity>) pageQuery.toMybatisPlusIPage(), new QueryWrapper<>(pageQuery.getFilter()))
 *        }
 *      }
 * </pre>
 *
 * @author: Li chaojie
 * @Date: 2021/4/16
 * @Description:
 */
public class PageQuery<T> {

    public long current = 1;

    public long size = 10;

    private T filter;

    private List<OrderItem> orders = new ArrayList<>();

    public T getFilter() {
        return filter;
    }

    public void setFilter(T filter) {
        this.filter = filter;
    }

    /**
     * 转mybatisplus分页模型
     *
     * @return
     */
    public <E> IPage<E> toMybatisPlusIPage() {
        Page<E> page = new Page<>(current, size);
        page.setOrders(orders);
        return page;
    }

    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public List<OrderItem> getOrders() {
        return orders;
    }

    public void setOrders(List<OrderItem> orders) {
        this.orders = orders;
    }
}
